package com.leyou.item.service;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.mapper.SpecMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpecService {
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    public Object queryGroupByCategory(Long cid) {
        SpecGroup record = new SpecGroup();
        record.setCid(cid);

        List<SpecGroup> specGroupList = this.specMapper.select(record);

        if (CollectionUtils.isEmpty(specGroupList)){
            throw new LyException(ExceptionEnum.SPECGROUP_NO_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);


    }

    //查询规格参数组内的规格参数
    public List<SpecParamDTO> querySpecParams(Long gid) {
        SpecParam record = new SpecParam();
             record.setGroupId(gid);
               List<SpecParam> specparamlist =specParamMapper.select(record);
               if (CollectionUtils.isEmpty(specparamlist)){
                   throw new LyException(ExceptionEnum.SPECPARAM_NOT_FOUND);
               }
               return BeanHelper.copyWithCollection(specparamlist,SpecParamDTO.class);
    }
}
